package com.goro;

import java.util.Collection;

import javax.ejb.EJB;

import com.joro.Customer;
import com.joro.Subscription;
import com.joro.Supplier;
import com.joro.ejb.CustomerFacadeLocal;
import com.joro.ejb.SubscriptionFacadeLocal;

public class CustomerEditBean {

	@EJB
	protected CustomerFacadeLocal customerDao;
	@EJB
	protected SubscriptionFacadeLocal subscriptionDao;
	
	private Customer customer;
	private int customerId;
	private String action;
	private Subscription subscription;

	public Subscription getSubscription() {
		return subscription;
	}

	public void setSubscription(Subscription subscription) {
		this.subscription = subscription;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;

		if (this.action != null && this.action.equals("edit")) {
			this.getCustomerData();
		}
	}

	public CustomerEditBean() {
		customer = new Customer();
	}

	public String getCustomerData() {
		if (this.customerId > 0) {
			// Init the customer member
			Customer tempCustomer = customerDao.getCustomerById(customerId);
			if (tempCustomer != null) {
				this.customer = tempCustomer;
			}
		}
		return null;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
		this.customer.setId(customerId);

	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public String saveCustomer() {		
		customerDao.saveCustomer(this.customer);		
		return "success";
	}

	public String deleteCustomer() {
		customerDao.deleteCustomer(customer);		
		return "success";
	}

	public String cancel() {
		return "success";
	}
	
	public Collection<Subscription> getCustomerSubscriptions(){
		return subscriptionDao.getCustomerSubscriptions(this.customer);
	}
	
	public String deleteSubscription(){
		subscriptionDao.deleteSubscription(subscription);
		return "successful_subscription_delete";
	}
}
